import { h, Component } from 'preact'
import Router from 'preact-router'
import AsyncRoute from 'preact-async-route'

const getComponent = (co) => (url, cb, props) => {
    const C = co()
    if (C.then) {
        return C.then(m => m.default)
    } else if (C.default) {
        cb({ component: C.default })
    }
}
const getLoading = (route) => {
    // if (this.state.ssrShown) {
    // console.info('=============loading')
    return <div>loading...</div>
    // }
    // else {
    //     return <div dangerouslySetInnerHTML={{ __html: this.state.ssrText }} />
    // }
}
export default ({ url, routes }) => (
    <Router url={url} >
        {routes.map((route) => (
            <AsyncRoute
                path={route.path}
                getComponent={getComponent(route.component)}
                //getComponent={route.component}
                loading={getLoading}
            />
        ))}
    </Router>
)


